#include "QQNavigationModel.h"

QQNavigationModel::QQNavigationModel(QObject* parent)
	: QAbstractListModel(parent)
{
	mSelectedNode = nullptr;
}

QQNavigationModel::~QQNavigationModel()
{
	qDeleteAll(mNodeList);
}

void QQNavigationModel::setSelectedNode(QQNavigationNode* node)
{
	mSelectedNode = node;
}

QQNavigationNode* QQNavigationModel::getSelectedNode() const
{
	return mSelectedNode;
}

QString QQNavigationModel::addPageNode(QIcon icon)
{
	return addPageNode(0, icon);
}

QString QQNavigationModel::addPageNode(int keyPoints, QIcon icon)
{
	QQNavigationNode* node = new QQNavigationNode;
	node->setNodeIcon(icon);
	node->setKeyPoints(keyPoints);
	this->beginResetModel();
	mNodeList.append(node);
	this->endResetModel();
	node->setModelIndex(this->index(mNodeList.count() - 1));
	return node->getNodeKey();
}

QQNavigationNode* QQNavigationModel::getNavigationNode(QString nodeKey) const
{
	for (int i = 0; i < mNodeList.size(); i++)
	{
		if (mNodeList.at(i)->getNodeKey() == nodeKey)
		{
			return mNodeList.at(i);
		}
	}
	return nullptr;
}

int QQNavigationModel::rowCount(const QModelIndex& parent) const
{
	return mNodeList.size();
};

QVariant QQNavigationModel::data(const QModelIndex& index, int role) const
{
	if (role == Qt::UserRole)
	{
		if (index.row() < mNodeList.size())
		{
			return QVariant::fromValue(mNodeList[index.row()]);
		}
	}
	return QVariant();
}